﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Drawing;

namespace Pixysoft.RTF.Core
{
    class RtfFontTable : IRtfFontTable
    {
        private readonly float _defaultFontSize;

        private int _font;
        private List<string> _fonttbl;

        private float _fontSize;
        private FontStyle _fontStyle;


        public RtfFontTable(RtfFont defaultFont, float defaultFontSize)
        {
            _fonttbl = new List<string>();

            this._font = IndexOfFont(defaultFont);
            this._defaultFontSize = defaultFontSize;

            this._fontSize = this._defaultFontSize;
            this._fontStyle = System.Drawing.FontStyle.Regular;
        }

        public void SetFont(int index)
        {
            if (index >= 0 && index < this._fonttbl.Count)
            {
                this._font = index;
            }
        }

        public void SetFont(RtfFont font)
        {
            this._font = IndexOfFont(font);
        }


        public int IndexOfFont(RtfFont font)
        {
            return IndexOfRawFont(RtfBuilder.RawFonts.GetKnownFontstring(font));
        }

        public int IndexOfRawFont(string font)
        {
            if (!string.IsNullOrEmpty(font))
            {
                int index = _fonttbl.IndexOf(font);
                if (index < 0)
                {
                    this._fonttbl.Add(font);
                    return _fonttbl.IndexOf(font);
                }
                return index;
            }
            return 0;
        }



        public int Font
        {
            get
            {
                return _font;
            }
            set
            {
                _font = value;
            }
        }

        public float FontSize
        {
            get
            {
                return _fontSize;
            }
            set
            {
                this._fontSize = value;
            }

        }

        public float DefaultFontSize
        {
            get
            {
                return _defaultFontSize;
            }
        }

        public FontStyle FontStyle
        {
            get
            {
                return _fontStyle;
            }
            set
            {
                _fontStyle = value;
            }
        }

        public string[] FontTableList
        {
            get
            {
                return _fonttbl.ToArray();
            }
        }
    }
}
